﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebAPI.Filter;
using WebAPI.Models.Database;
using WebAPI.Service;

namespace WebAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ResultFilter]
    public class GoodController : ControllerBase
    {
        //定义数据库上下文
        private readonly Store_2022Context _db;
        private readonly ISingletonService _singletonService;
        private readonly ITransientService _transientService;
        private readonly IScopedService _scopedService;
        /// <summary>
        /// 构造方法 构造注入
        /// </summary>
        /// 
        public GoodController(Store_2022Context db, ISingletonService singletonService, ITransientService transientService,IScopedService scopedService)
        {
            _db = db;
            _transientService = transientService;
            _singletonService = singletonService;
            _scopedService = scopedService;
        }
        /// <summary>
        /// 新增产品
        /// </summary>
        /// <param name="good"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddGood(Good good)
        {
            _db.Goods.Add(good);
            var row = _db.SaveChanges();
            if (row > 0)
                return "新增成功";
            return "新增失败";
        }
        /// <summary>
        /// 新增产品 自定请求类
        /// 站点名/api/Good/AddGood2
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string AddGood2(AddGood2 request)
        {
            //开始 业务逻辑

            //手动转换类 把请求类 转换 成 数据库模型类
            if (!ModelState.IsValid)
            {
                return "数据错误";
            }
            var good = new Good
            {
                Name = request.Name,
                Cover = request.Cover,
                CateId = request.CateId,
                Price = request.Price,
                Stock = request.Stock,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
            };

            //数据库上下.操作的表名.操作的方法(参数)
            _db.Goods.Add(good);
            //保存更改 row影响条数
            var row = _db.SaveChanges();
            //数据库新增成功了
            if (row > 0)
                return "新增成功";
            return "新增失败";
        }

        //第三种方式 URL参数
        //api/good/AddGood3?name=xxxx&cateid=2&cover=xxxx   name cover  price stock
        [HttpGet]
        public string AddGood3(string name, string cover, int cateId, decimal price, int stock)
        {
            //把参数 转换 成商品模型类
            var good = new Good
            {
                Name = name,
                CateId = cateId,
                Cover = cover,
                Price = price,
                Stock = stock,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
            };
            //数据库上下.操作的表名.操作的方法(参数)
            _db.Goods.Add(good);
            //保存更改 row影响条数
            var row = _db.SaveChanges();
            //数据库新增成功了
            if (row > 0)
                return "新增成功";
            return "新增失败";
        }
        [HttpPost]
        public string UpdateGood(UpdateGood request)
        {
            //6业务逻辑 根据需求 更新哪个商品 更新成什么
            //查询出需要更新的商品 根据条件查询第一个商品
            var good = _db.Goods.FirstOrDefault(x => x.Id == request.CateId);
            if (good == null)
                return "商品为空";

            //把前端传过来数据赋值
            good.Name = request.Name;
            good.Cover = request.Cover;
            good.CateId = request.CateId;
            good.Price = request.Price;
            good.Stock = request.Stock;
            good.UpdateTime = DateTime.Now;

            //数据库更新操作
            _db.Goods.Update(good);
            var row = _db.SaveChanges();
            if (row > 0)
                return "更新成功";
            return "更新失败";
        }

        //删除商品  
        /// <summary>
        /// 删除商品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string DeleteGood(int id)
        {
            //删除哪个商品 查询指定商品
            var good = _db.Goods.FirstOrDefault(x => x.Id == id);
            if (good == null)
                return "商品为空";
            _db.Remove(good);
            var row = _db.SaveChanges();
            if (row > 0)
                return "删除成功";
            return "删除失败";
        }
        [HttpGet]
        //public List<Good> GetGoodList1(GetGoodList1Request request)
        //{
        //    //获取商品操作
        //    var list = _db.Goods.AsQueryable();
        //    //如果查询的分类Id>0 
        //    if (request.CateId > 0)
        //        list = list.Where(x => x.CateId == request.CateId);
        //    //如果查询的关键字不为空
        //    if (!string.IsNullOrEmpty(request.Keyword))
        //        list = list.Where(x => x.Name.Contains(request.Keyword));
        //    return list.ToList();
        //}

        /// <summary>
        /// 根据Get参数获取产品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [CustomerResourceFilterAttribute]
        public List<Good> GetGoodAllList2(string keyword, int cateId)
        {
            //获取商品操作
            var list = _db.Goods.AsQueryable();
            //如果查询的分类Id>0 
            if (cateId > 0)
                list = list.Where(x => x.CateId == cateId);
            //如果查询的关键字不为空
            if (string.IsNullOrEmpty(keyword))
                list = list.Where(x => x.Name.Contains(keyword));
            return list.ToList();
        }
        [HttpPost]
        public string Singleton([FromServices] ISingletonService singletonService)
        {
            return "1的hashcode=" + _singletonService.GetHashCode() + "  2的hashcode=" + singletonService.GetHashCode();
        }
    }
}
